package com.swm.util;

import com.alibaba.fastjson2.JSONObject;
import com.swm.mapper.StockDayDataMapper;
import com.swm.mapper.StockMapper;
import com.swm.pojo.Stock;
import com.swm.pojo.Stock_Day_Data;
import org.apache.ibatis.session.SqlSession;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 师文明
 * @data 2024/12/23 下午3:11
 **/
public class getStockDayData491 {

    public static DecimalFormat df = new DecimalFormat("#0.000");
    public static int ban = 0;
    static SqlSession sqlSession = SqlSessionUtil.openSession();
    static StockMapper mapper = sqlSession.getMapper(StockMapper.class);
    static StockDayDataMapper stockDayDataMapper = sqlSession.getMapper(StockDayDataMapper.class);

    private static ArrayList<String> dateList = new ArrayList<String>();
    public static List<Stock> getStcokList() {
        List<Stock> stocks = mapper.selectAllStock();
        return stocks;
    }

    public static void main(String[] args) throws Exception {
        List<Stock_Day_Data> stockDayData = stockDayDataMapper.selectAllStockDayData();
        System.out.println(stockDayData.size());
        if(stockDayData.size()>0){
            ExecutorService executorService = Executors.newFixedThreadPool(10);
            //获取所有股票列表
            List<Stock> stcokList = getStcokList();
            for (Stock stock : stcokList) {
                executorService.execute(() -> {
                    try {
                        getData(stock,stockDayData);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                });
            }
            Thread.sleep(150000);



        }

    }

    public static void getData(Stock stock,List<Stock_Day_Data> stockDayData1) throws Exception {
        // System.out.println(count);
        // List<Stock_Day_Data> stockDayData = stockDayDataMapper.selectStockDayData(stock.getId());
        List<Stock_Day_Data> stockDayData = new ArrayList<Stock_Day_Data>();
        stockDayData1.forEach(stockDayData2 -> {
            if(stockDayData2.getCode().equals(stock.getCode())){
                stockDayData.add(stockDayData2);
            }
        });
        Collections.sort(stockDayData, new Comparator<Stock_Day_Data>() {
            @Override
            public int compare(Stock_Day_Data o1, Stock_Day_Data o2) {
                return o2.getGetdate().compareTo(o1.getGetdate());
            }
        });
            if(stockDayData.size()>120) {
                int i = 0;
                //当日涨幅
                double v= stockDayData.get(i).getZdf();

                JSONObject lines = getLines(stockDayData, i);
                double f1 = Double.parseDouble(lines.get("f1").toString());
                double te1 = Double.parseDouble(lines.get("te1").toString());
                double tw1 = Double.parseDouble(lines.get("tw1").toString());
                double th1 = Double.parseDouble(lines.get("th1").toString());
                double six1 = Double.parseDouble(lines.get("six1").toString());

                //开盘价
                double k = stockDayData.get(i).getK();

                //收盘价
                double s = stockDayData.get(i).getS();

                //最高价
                double g = stockDayData.get(i).getG();
                //最低价
                double d = stockDayData.get(i).getD();

                double k1 = stockDayData.get(i + 1).getK();

                //收盘价
                double s1 = stockDayData.get(i + 1).getS();

                //最高价
                double g1 = stockDayData.get(i + 1).getG();
                //最低价
                double d1 = stockDayData.get(i + 1).getD();


                double k2 = stockDayData.get(i + 2).getK();

                //收盘价
                double s2 = stockDayData.get(i + 2).getS();
                double k3 = stockDayData.get(i + 3).getK();

                //收盘价
                double s3 = stockDayData.get(i + 3).getS();
                double k4 = stockDayData.get(i + 4).getK();

                //收盘价
                double s4 = stockDayData.get(i + 4).getS();
                double k5 = stockDayData.get(i + 5).getK();

                //收盘价
                double s5 = stockDayData.get(i + 5).getS();


                //当天振幅
                double v7 = (s - k) * 100 / k;


                boolean bf = d < f1 && s > f1;
                boolean bte = d < te1 && s > te1;
                boolean btw = d < tw1 && s > tw1;
                boolean bth = d < th1 && s > th1;
                boolean bsix = d < six1 && s > six1;
                String word = "";
                int flag = 0;
                if (bf) {
                    word = word.concat("上穿五日线");
                    flag++;
                }
                if (bte) {
                    word = word.concat("上穿10日线");
                    flag++;
                }
                if (btw) {
                    word = word.concat("上穿20日线");
                    flag++;
                }
                if (bth) {
                    word = word.concat("上穿30日线");
                    flag++;
                }
                if (bsix) {
                    word = word.concat("上穿60日线");
                    flag++;
                }


                //收盘与最高差距
                double v0000 = (g - s) * 100 / k;
                boolean price12010 = getPrice12010(stockDayData, i);
                boolean price108 = getPrice108(stockDayData, i);
                if(v < 9.8 && v > 0){
                    //五天阳线涨幅
                    double v2 = (s1 - k5) * 100 / k5;
                    if (v0000 < 0.2 && v7 > 3 && s1 > k1 && s2 > k2 && s3 > k3 && s4 > k4 && s5 > k5 && price12010 && v2 > 8 && v2 < 12) {
                        System.out.println(stockDayData.get(i).getName() + "120天底部出现连续五根小阳线，加起来一共10个点左右，第6天突破基本没有上影线");
                    }
                    if(price12010&&price108){
                        System.out.println(stockDayData.get(i).getName() + "120天内小于10， 10天内大于8==9");
                    }
                }

            }

    }

    //获取所有均线
    public static JSONObject getLines(List<Stock_Day_Data> stockDayData, int startDay) {
        //日线
        double totalsix1 = 0.0;
        double totalth1 = 0.0;
        double totaltw1 = 0.0;
        double totalte1 = 0.0;
        double totalf1 = 0.0;
        for (int j = 0; j < 60; j++) {
            double price = stockDayData.get(startDay + j).getS();

            if (j < 5) {
                totalf1 += price;
            }
            if (j < 10) {
                totalte1 += price;
            }
            if (j < 20) {
                totaltw1 += price;

            }
            if (j < 30) {
                totalth1 += price;

            }
            totalsix1 += price;

        }
        double six1 = totalsix1 / 60;
        double th1 = totalth1 / 30;
        double tw1 = totaltw1 / 20;
        double te1 = totalte1 / 10;
        double f1 = totalf1 / 5;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("f1", f1);
        jsonObject.put("te1", te1);
        jsonObject.put("tw1", tw1);
        jsonObject.put("th1", th1);
        jsonObject.put("six1", six1);
        return jsonObject;

    }



    //近120天 价格处于低位
    public static boolean getPrice12011(List<Stock_Day_Data> stockDayData, int startDay) {
        //最近120天内收盘价集合
        List<Double> prices = new ArrayList<>();
        double s1 = stockDayData.get(startDay + 1).getS();
        double s2 = stockDayData.get(startDay).getS();
        for (int j = 0; j < 120; j++) {
            //收盘价
            double p2 = stockDayData.get(startDay + j).getS();
            prices.add(p2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        prices.sort(Double::compare);
        return s2 < prices.get(11)/*s1==prices.get(0)*/;
    }



    //近10天 价格处于高位
    public static boolean getPrice108(List<Stock_Day_Data> stockDayData, int startDay) {
        List<Double> prices = new ArrayList<>();
        double s1 = stockDayData.get(startDay + 1).getS();
        double s2 = stockDayData.get(startDay).getS();
        for (int j = 0; j < 10; j++) {
            //收盘价
            double p2 = stockDayData.get(startDay + j).getS();
            prices.add(p2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        prices.sort(Double::compare);
        return s2 > prices.get(8)/*s1==prices.get(0)*/;
    }
    //近120天 价格处于低位
    public static boolean getPrice12010(List<Stock_Day_Data> stockDayData, int startDay) {
        //最近120天内收盘价集合
        List<Double> prices = new ArrayList<>();
        double s1 = stockDayData.get(startDay + 1).getS();
        double s2 = stockDayData.get(startDay).getS();
        for (int j = 0; j < 120; j++) {
            //收盘价
            double p2 = stockDayData.get(startDay + j).getS();
            prices.add(p2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        prices.sort(Double::compare);
        return s2 < prices.get(10)/*s1==prices.get(0)*/;
    }
}

